package com.ifast.wxmp.service;

import org.springframework.stereotype.Service;

import com.ifast.wxmp.config.Constants;
import com.ifast.wxmp.config.WxMpConfiguration;

import me.chanjar.weixin.common.api.WxConsts.MenuButtonType;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.error.WxErrorException;

@Service
public class CustomMenuService {

	public void createMenu(String appId) {
		try {
			// 第一个菜单
			WxMenu menu = new WxMenu();
			WxMenuButton button1 = new WxMenuButton();
			button1.setType(MenuButtonType.VIEW);// MenuButtonType.CLICK
			button1.setName("极客冲顶");
			button1.setUrl("http://www.baidu.com/");

			// 第二个菜单
			WxMenuButton button2 = new WxMenuButton();
			button2.setType(MenuButtonType.VIEW);
			button2.setName("精选文章");
			button2.setUrl("http://www.baidu.com/");
			
			WxMenuButton button21 = new WxMenuButton();
			button21.setType(MenuButtonType.VIEW);
			button21.setName("历史文章");
			button21.setUrl(Constants.article_url);
			button2.getSubButtons().add(button21);
			
			// 第三个菜单
			WxMenuButton button3 = new WxMenuButton();
			button3.setName("在线交流");

			menu.getButtons().add(button1);
			menu.getButtons().add(button2);
			menu.getButtons().add(button3);

			WxMenuButton button31 = new WxMenuButton();
			button31.setType(MenuButtonType.VIEW);
			button31.setName("关于我们");
			button31.setUrl("http://www.soso.com/");

			WxMenuButton button33 = new WxMenuButton();
			button33.setType(MenuButtonType.CLICK);
			button33.setName("赞一下我们");
			button33.setKey("V1001_GOOD");

			button3.getSubButtons().add(button31);
			button3.getSubButtons().add(button33);

			WxMpConfiguration.getMpServices().get(appId).getMenuService().menuCreate(menu);
		} catch (WxErrorException e) {
			e.printStackTrace();
		}
	}
}
